<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\Amodule;
class AmoduleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    //分类列表
    public function index()
    {
        $Amodule = Amodule::orderBy('mname','asc')->get();
        $Amodule = new Amodule();
        $data = $Amodule->tree();
        return view('admin.amodule.index',['data'=>$data]);
        // return view('admin.amodule.xxoo');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    //添加分类
    public function create()
    {
        //添加分类 
        // $data = Amodule::orderBy('mname','asc')->where('pic','0')->get();
        // dump($data);
        $Amodule = new Amodule();
        $data = $Amodule->tree();
        return view('admin.amodule.add',['data'=>$data]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    //添加分类
    public function store(Request $request)
    {
        
        $input = $request->except('_token');
        $re = Amodule::create($input);
        // dump($input);
        // $Amodule->pic = $input['pic'];
        // $Amodule->mname = $input['mname'];
        // $Amodule->classify = $input['classify'];
        // $Amodule->order = $input['order'];
        // $re = $Amodule->save();
        if($re){
            return redirect('admin/amodule');
        }else{
            return back()->with('msg','修改失败');
        }

    }

    /**
     * Display the specified resource.z
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    //
    ////删除分类
    public function show($id)
    {
         $Amodule = Amodule::find($id);
         $re = $Amodule -> delete();
        if($re){
            return redirect('admin/amodule');
        }else{
            return back()->with('msg','修改失败');  
        }
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     *///分类列表
    public function edit($id)
    {
        $Amodule = new Amodule();
        $datas = $Amodule->tree();
        $data = Amodule::find($id);
        // $data = $data->tree();
        return view('admin.amodule.edit',['data'=>$data,'datas'=>$datas]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    //处理提交的修改的分类
    public function update(Request $request, $id)
    {
        $data = Amodule::find($id);
        //var_dump($data);
        $input = $request->except('_token','_method');

        //        dump($input);
        $data = $data->update($input);
        if($data){
            return redirect('admin/amodule');
        }else{
            return back()->with('msg','修改失败');
        }
        //var_dump($data);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
